package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.ChrQueryUsersCmd;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * 提供vp查询USER
 *
 * @author WSL
 * @create 2017/12/13
 */
@Slf4j
@Component("UsersWebQueryCmdImpl-newcp")
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ChusersWebQueryCmdImpl extends CommandAdapter implements ChrQueryUsersCmd {

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat
                        (map.get("param"),"yyyy-MM-dd HH-mm-ss", SerializerFeature.WriteMapNullValue),
                Feature.OrderedField);

        try {
            User user = Tools.getUser(param.getString("name"));
            byte[] userB = serializer.serialize(user);

            vh.put("user",new String(userB, "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return vh;
    }
}
